//
//  MoneTaskVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/1/19.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "MoneTaskVC.h"
#import "ProgressView.h"
#import "MyVM.h"
@interface MoneTaskVC ()
@property (nonatomic ,strong) NSMutableArray *dataArr;
@end

@implementation MoneTaskVC{
    float   total;
    float   CompletedCount;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"赚钱任务";
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithTitle:@"规则" style:UIBarButtonItemStylePlain target:self action:@selector(onclickRules)];
    self.navigationItem.rightBarButtonItem=rightItem;
    [self getData];
}

- (void)getData{
    WS(weakSelf);
    [[MyVM manage] getTaskListSuccess:^(id returnValue) {
        weakSelf.dataArr=returnValue;
        [weakSelf updateUI];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

- (void)onclickRules{
    PublicWebVC *vc=[[PublicWebVC alloc]init];
    vc.url=@"http://mobile.c8.cn/Personal/TaskRule";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)updateUI{
    for (int i=0; i<self.dataArr.count; i++) {
        TaskModel *model=self.dataArr[i];
        
        BOOL isDone=([model.CompletedCount intValue]==[model.Count intValue]);
        total+=[model.Coin floatValue];
        if (isDone) {
            CompletedCount+=[model.Coin floatValue];
        }
        UIView *view=[self taskViewWithContent:[NSString stringWithFormat:@"%@(%@/%@)",model.TaskItem,model.CompletedCount,model.Count] andMoney:model.Coin andDone:isDone andBottom:(i==self.dataArr.count-1)];
        
        view.frame=CGRectMake(0, 240+i*40, SCREEN_WIDTH, 40);
        [self.view addSubview:view];
    }
    [self headView];
    
    UIView *titleView=[[UIView alloc]initWithFrame:CGRectMake(0, 205, SCREEN_WIDTH, 35)];
    titleView.backgroundColor=[UIColor whiteColor];
    QXLable *labTitle=[[QXLable alloc]initTitle];
    labTitle.text=@"赚钱任务";
    [titleView addSubview:labTitle];
    [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleView).with.offset(10);
        make.centerY.equalTo(titleView);
    }];
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [titleView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(labTitle);
        make.right.bottom.equalTo(titleView);
        make.height.mas_equalTo(@.7);
    }];
    [self.view addSubview:titleView];
}


- (void)headView{
    UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    headView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:headView];
    ProgressView *bgProgressView=[[ProgressView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-200)/2, 30, 200, 200)];
    [bgProgressView drawProgress:1.0 andColor:DEFAULTBGCOLOR];
    [headView addSubview:bgProgressView];
    
    ProgressView *progressView=[[ProgressView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-200)/2, 30, 200, 200)];
    [progressView drawProgress:CompletedCount/total andColor:NAVCOLOR];
    [headView addSubview:progressView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 140)];
    label.textAlignment = NSTextAlignmentCenter;
    label.center =CGPointMake(progressView.center.x, progressView.center.y+13);
    label.numberOfLines=0;
    label.textColor=[UIColor blackColor];
    NSMutableAttributedString *attStr=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%.0f\n\n已领取\n\n今日可领%.0f金币",CompletedCount,total]];
    [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 1)];
    [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, 1)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DEFAULTTEXTGRAYCOLOR range:NSMakeRange(attStr.length-8, 8)];
    [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(attStr.length-8, 8)];
    label.attributedText=attStr;
    [headView addSubview:label];
}

- (UIView *)taskViewWithContent:(NSString *)content andMoney:(NSString *)money andDone:(BOOL)isDone andBottom:(BOOL)isBottom{
    UIView *taskView=[[UIView alloc]init];
    taskView.backgroundColor=[UIColor whiteColor];
    UIView *roundView=[[UIView alloc]init];
    roundView.layer.masksToBounds=YES;
    roundView.layer.cornerRadius=6.0;
    roundView.layer.borderColor=DEFAULTTEXTGRAYCOLOR.CGColor;
    roundView.layer.borderWidth=1.0;
    [taskView addSubview:roundView];
    [roundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(taskView);
        make.left.equalTo(taskView).with.offset(13);
        make.size.mas_equalTo(CGSizeMake(12, 12));
    }];
    
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [taskView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(roundView);
        make.top.equalTo(taskView);
        make.bottom.equalTo(roundView.mas_top);
        make.width.mas_equalTo(@1);
    }];
    
    UIView *line1=[[UIView alloc]init];
    line1.backgroundColor=DEFAULTBGCOLOR;
    line1.hidden=isBottom;
    [taskView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(roundView);
        make.bottom.equalTo(taskView);
        make.top.equalTo(roundView.mas_bottom);
        make.width.mas_equalTo(@1);
    }];
    
    QXLable *labTitle=[[QXLable alloc]init];
    labTitle.text=content;
    [taskView addSubview:labTitle];
    [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(roundView.mas_right).with.offset(5);
        make.centerY.equalTo(taskView);
        make.size.mas_equalTo(CGSizeMake(150, 30));
    }];
    
    QXLable *labMoney=[[QXLable alloc]initWithFount:10];
    labMoney.text=[NSString stringWithFormat:@"+%@金币",money];
    [taskView addSubview:labMoney];
    [labMoney mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(taskView).with.offset(-10);
        make.centerY.equalTo(taskView);
    }];
    
    if (isDone) {
        UIImageView *imageV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
        [taskView addSubview:imageV];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(taskView).with.offset(-90);
            make.centerY.equalTo(taskView);
            make.size.mas_equalTo(imageV.image.size);
        }];
        labMoney.textColor=NAVCOLOR;
    }
    
    return taskView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
